import wsgiref.handlers
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from treatpouch import Treat

class WritePage(webapp.RequestHandler):
	def get(self, *args):
                user = users.get_current_user()
                if user:
                        key_str = args[0]
                        key_str = key_str.replace("page/", "")
                        treat = db.get(key_str)
                        if (user == treat.author):
                                url = users.create_logout_url(self.request.uri)
                                template_values = {
                                    'title': treat.title,
                                    'page': treat.page, 
                                    'logout': url,
                                    'treat_key': treat.key(),
                                }    
                                self.response.out.write(template.render('templates/page.html', template_values))
                        else:
                                self.response.out.write("<p>Incorrect user</p>")
		else:
                        self.redirect(users.create_login_url(self.request.uri))
             
app = webapp.WSGIApplication([
  ('/(.*)', WritePage)
], debug=True)
		
def main():
 wsgiref.handlers.CGIHandler().run(app)
  
if __name__ == '__main__':
        main()
	
	
	
	
